package com.bacel.nb.product.web.template.method;

import freemarker.template.SimpleNumber;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;

import java.util.List;

/**
 * http://www.cnblogs.com/beiyeren/p/3873683.html
 */
public class IndexOfMethod implements TemplateMethodModelEx {
    @Override
    public Object exec(List list) throws TemplateModelException {
        if(list.size() != 2)
            throw new TemplateModelException("Wrong argments !");

        SimpleScalar src = (SimpleScalar)(list.get(1));
        SimpleScalar token = (SimpleScalar)(list.get(0));

        return new SimpleNumber(src.getAsString().indexOf(token.getAsString()));
    }
}
